//
//  ProductDownView.m
//  MJApp
//
//  Created by wangliang on 15/3/16.
//  Copyright (c) 2015年 wangliang. All rights reserved.
//

#import "ProductDownView.h"
#import "ProductDownCollectionViewCell.h"

const CGFloat kItemWide = 47;
const CGFloat kBetween = 12;

@interface ProductDownView ()<UICollectionViewDataSource,UICollectionViewDelegate>
{
    NSInteger _index;
}
@property(nonatomic,strong)UICollectionView *collectionView;
@property(nonatomic,strong)ProductDownCollectionViewCell *lastCell;

@end

@implementation ProductDownView

- (instancetype)initWithFrame:(CGRect)frame {
    
    if (self = [super initWithFrame:frame]) {
        
        [self UI];
    }
    return self;
}
- (void)setSelectedPage:(NSInteger)selectedPage{

    _selectedPage = selectedPage;
    _index = selectedPage;
    [self.collectionView reloadData];
}

- (void)UI {
    
    _index = 0;
    UICollectionViewFlowLayout *flowLayOut = [[UICollectionViewFlowLayout alloc] init];
    flowLayOut.itemSize = CGSizeMake(kItemWide, kItemWide);
    flowLayOut.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    flowLayOut.sectionInset = UIEdgeInsetsMake(7, 12, 7, 12);
    flowLayOut.minimumLineSpacing = kBetween;
    CGRect frame = self.frame;
    frame.size.width = IOS_IPHONE_WINDOW_WIDE  - 36;
    self.collectionView = [[UICollectionView alloc] initWithFrame:frame collectionViewLayout:flowLayOut];
    [self.collectionView registerNib:[UINib nibWithNibName:@"ProductDownCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"ProductDownCollectionViewCell"];
//    self.collectionView.backgroundColor = [UIColor redColor];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self addSubview:self.collectionView];
    
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 12, 20)];
    button.center = CGPointMake(IOS_IPHONE_WINDOW_WIDE - 18, CGRectGetHeight(self.frame) / 2);
    [button setImage:LOAD_IMAGE(@"sc11_49.png") forState:UIControlStateNormal];
    [self addSubview:button];
    [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return self.imagesArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    ProductDownCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ProductDownCollectionViewCell" forIndexPath:indexPath];
    if (indexPath.row == _index) {
        
        cell.isSelect = YES;
        self.lastCell = cell;
    }else {
        
        cell.isSelect = NO;
    }
    cell.imageDic = self.imagesArr[indexPath.row];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    _index = indexPath.row;
    if ([self.delegate respondsToSelector:@selector(ProductDownViewDidSelectedWithProductDownView:andIndex:)]) {
        [self.delegate ProductDownViewDidSelectedWithProductDownView:self andIndex:indexPath.row];
    }
    [self.collectionView reloadData];
}

- (void)buttonAction:(UIButton *)sender {
    
    if (_index + 1 < [self.collectionView numberOfItemsInSection:0]) {
        _index ++;
        [self.collectionView reloadData];
    }
    
    CGPoint currPoint = self.collectionView.contentOffset;
    CGFloat wide = CGRectGetWidth(self.collectionView.frame);
    CGFloat sizeWide = self.collectionView.contentSize.width;
    
    if (currPoint.x + wide <= sizeWide - kItemWide) {
        
        currPoint.x += (kBetween + kItemWide);
        [self.collectionView setContentOffset:currPoint animated:YES];
    }else
    {
        
        //最后移动部分
        CGFloat move = (currPoint.x + wide) - (sizeWide - kItemWide);
        if (move < kItemWide) {
            
            currPoint.x += (kItemWide - move);
            [self.collectionView setContentOffset:currPoint animated:YES];
        }
    }
    
    
}

@end
